if /I "%1" == "-h" (
	call :usage
	exit /B 1
)
call common\function.bat parse_args %*

if not !errorlevel! EQU 0 exit /B 1

if not exist !j_cmake! (
	echo "cmake not found. Try 'jenny update' please"
	exit /B 1
)

set j_defines=-DPROJECT_FILE=!j_config_path! -DCMAKE_BUILD_TYPE=!j_config! -DJ_TARGET_PLATFORM=!j_platform!  -DJ_ARCHITECTURE=!j_architecture! -DJ_TOOLSET=!j_toolset! -DJ_TOOLSET_VER=!j_toolset_ver!

for /F "usebackq delims=#" %%i in (`!j_cmake! !j_defines! -P !j_jenny_root_dir!/cmake/prepare.cmake`) do (
    set lvar=%%i
	set !lvar:~3!
)
set j_generated_project_dir=!j_generated_project_dir:/=\!
if not exist "!j_generated_project_dir!" (
	echo Failed to locate generated project dir: !j_generated_project_dir!
	exit /B 1
)
set j_cmake_generator=-G "!j_generator!"

pushd "!j_generated_project_dir!"
echo Project dir: !CD!
:: Generate
!j_cmake! !j_defines! !j_cmake_generator!  !j_jenny_root_dir!/cmake
if not !errorlevel! EQU 0 (
	echo Failed to generate project.
	exit /B 1
)
popd

!j_cmake! --build "!j_generated_project_dir!" --config !j_config! -- /maxcpucount /v:m 

exit /B

:usage
	echo "Usage: jenny build [options] [path]"
	echo "options:"
	echo "    -c|--config   <debug|release>     	debug is default"
	echo "    -a|--arch     <x64|x86>				x64 is default"
	echo "    -t|--toolset  <vc14|vc126>			vc14 is default"
	echo "    -d|--target  							default is empty, means all"
	echo "    -h|--help"
	exit /B 1
